using Mathmall.Administration.common;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace Mathmall.Administration.Controllers;

[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
    private readonly IRoleService _roleService;
    private readonly IRoleMenuService _roleMenuService;

    public RoleController(IRoleService roleService, IRoleMenuService roleMenuService)
    {
        _roleService = roleService;
        _roleMenuService = roleMenuService;
    }

    /// <summary>
    /// 添加角色
    /// </summary>
    /// <param name="roleName"></param>
    /// <returns></returns>
    [Authorize(Policy = "AddRole")]
    [HttpPost("[action]/{roleName}")]
    public async Task<IdentityResult> AddRole(string roleName)
    {
        return await _roleService.AddRole(roleName);
    }

    /// <summary>
    /// 分页查询全部角色
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="condition"></param>
    /// </summary>
    /// <returns></returns>
    [Authorize(Policy = "QueryRole")]
    [HttpGet("{pageIndex}/{pageSize}/{condition}")]
    public async Task<PaginatedList<IdentityRole<int>>> PaginatedQueryRoles(int pageIndex, int pageSize,
        string condition = "")
    {
        return await _roleService.PageNationQuery(pageIndex, pageSize, condition);
    }

    /// <summary>
    /// 查询全部角色
    /// </summary>
    /// <returns></returns>
    [Authorize(Policy = "QueryRole")]
    [HttpGet]
    public async Task<List<IdentityRole<int>>> GetRoles()
    {
        return await _roleService.QueryAllRoles();
    }

    /// <summary>
    /// 更新角色名称
    /// </summary>
    /// <param name="identityRole"></param>
    /// <returns></returns>
    [Authorize(Policy = "EditRole")]
    [HttpPost]
    public async Task<IdentityResult> UpdateRole(IdentityRole<int> identityRole)
    {
        return await _roleService.UpdateRole(identityRole);
    }

    /// <summary>
    /// 为角色添加菜单
    /// </summary>
    /// <returns></returns>
    [Authorize(Policy = "AssignPermission")]
    [HttpPost("[action]")]
    public async Task<int> AddMenuToRole(RoleMenuVo roleMenuVo)
    {
        return await _roleMenuService.AddRoleMenuAsync(roleMenuVo);
    }

    /// <summary>
    /// 查询角色已有权限
    /// </summary>
    /// <param name="roleId"></param>
    /// <returns></returns>
    [Authorize(Policy = "QueryRole")]
    [HttpGet("{roleId}")]
    public async Task<List<int>> GetRolePossessedMenuIds(int roleId)
    {
        return await _roleMenuService.GetRolePossessedMenuIds(roleId);
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="identityRole"></param>
    /// <returns></returns>
    [Authorize(Policy = "DeleteRole")]
    [HttpDelete]
    public async Task<ActionResult> DeleteRole(IdentityRole<int> identityRole)
    {
        // 删除角色
        await _roleService.DeleteRole(identityRole);
        // 删除角色对应的权限
        await _roleMenuService.DeleteRoleMenu(identityRole.Id);
        return Ok();
    }
}